<template>
  <view class="viewpoint">
    <!-- 个人信息 -->
    <view class="userInfo" :style="{ paddingTop: safeAreaInsets!.top + 'px'}">
      <view class="contain" v-if="memberStore.profile">
        <view class="Info">
          <!-- 用户头像 -->
          <view class="avatar">
            <image :src="memberStore.profile?.user.avatar" />
          </view>
          <!-- 用户信息 -->
          <view class="Info_item">
            <view class="A"
              ><text>{{ memberStore.profile?.user.username }}</text></view
            >
            <view class="B"
              ><text>已发布:{{ productnum }}篇</text></view
            >
          </view>
        </view>
        <view class="introduce C">
          <text>{{ memberStore.profile?.user.userInfo }}</text>
        </view>
        <view class="address C">
          <text v-if="memberStore.profile?.user.address"
            >IP:{{ memberStore.profile?.user.address }}</text
          >
          <text v-else>IP:未填写</text>
          <text v-if="memberStore.profile?.user.sex" style="margin-left: 10rpx"
            >性别：{{ memberStore.profile?.user.sex == '0' ? '男' : '女' }}</text
          >
          <text style="margin-left: 10rpx" v-else>性别:未填写</text>
        </view>
        <view class="profile C" @tap="modifyInfo"
          ><text>编辑资料 <text class="iconfont icon-xiangyou1"></text></text>
        </view>
      </view>
      <view class="empty" v-else>
        <view class="defaulimg"><image src="../../static/images/person_2.png" /></view>
        <view class="unlogin" @tap="toLogin">去登录</view>
      </view>
      <view class="setting" v-if="memberStore.profile" @tap="toggle"
        ><text class="iconfont icon-shezhi"></text
      ></view>
    </view>
    <view class="tabBar">
      <view :class="index == 0 ? 'works active' : 'works'" @tap="index = 0">作品</view>
      <view :class="index == 1 ? 'favourate active' : 'favourate'" @tap="index = 1">收藏</view>
      <view :class="index == 2 ? 'draft active' : 'draft'" @tap="index = 2">草稿</view>
      <view :class="index == 3 ? 'like active' : 'like'" @tap="index = 3">点赞</view>
      <view :class="index == 4 ? 'history active' : 'history'" @tap="index = 4">历史</view>
    </view>
    <view class="produce">
      <swiper @change="SwiperChange" :current="index" v-if="memberStore.profile">
        <swiper-item v-for="item in 5" :key="item">
          <scroll-view v-if="productList?.length != 0" scroll-y>
            <!-- 用户作品 -->
            <view
              class="works"
              @tap="productDetail(items)"
              v-for="items in productList"
              :key="items.newsId"
            >
              <!-- 用户信息 -->
              <view class="user">
                <view class="avatar">
                  <image v-if="items.avatar != null" :src="items.avatar" />
                  <image v-else :src="memberStore.profile?.user.avatar" />
                </view>
                <view class="userinfo">
                  <view v-if="items.username != null">{{ items.username }}</view>
                  <view v-else>{{ memberStore.profile?.user.username }}</view>
                  <view>{{ formatTime(items.releaseTime) }}</view>
                </view>
              </view>
              <!-- 新闻信息 -->
              <view class="news">
                <view class="title">
                  {{ items.title }}
                </view>
                <view class="cover"><image :src="items.newsImgArr[0]" /></view>
                <view class="static P" v-if="items.status == 1">已上架</view>
                <view class="static D" v-if="items.status == 0">审核中</view>
              </view>
            </view>
          </scroll-view>
          <view v-else class="empty_swiper">
            <view class="defaulimg_swiper"><image src="../../static/images/empty.png" /></view>
            <view class="unlogin_swiper" @tap="toLogin">空空如也</view>
          </view>
        </swiper-item>
      </swiper>
      <swiper @change="SwiperChange" :current="index" v-else>
        <swiper-item v-for="item in 5" :key="item">
          <view class="empty_swiper">
            <view class="defaulimg_swiper"><image src="../../static/images/empty.png" /></view>
            <view class="unlogin_swiper" @tap="toLogin">去登录</view>
          </view>
        </swiper-item>
      </swiper>
    </view>
  </view>
  <view>
    <!-- 普通弹窗 -->
    <uni-popup ref="popup" background-color="#fff">
      <view class="setting_select" :style="{ paddingTop: safeAreaInsets!.top + 'px'}">
        <view class="setting_contain">
          <view class="Q" @tap="modifyInfo">
            <text class="iconfont icon-yidongduan_bianjiziliao"></text>
            <text class="selection">编辑资料</text>
          </view>
          <view class="Q" @tap="repassword">
            <text class="iconfont icon-xiugaimima"></text>
            <text class="selection">修改密码</text>
          </view>
          <view class="Q" @tap="Loginout">
            <text class="iconfont icon-tuichudenglu" style="color: #f04142"></text>
            <text class="selection" style="color: #f04142">退出登录</text>
          </view>
        </view>
      </view>
    </uni-popup>
  </view>
</template>

<script setup lang="ts">
import { onLoad, onShow, onTabItemTap } from '@dcloudio/uni-app'
import { useMemberStore } from '@/stores/index'
import {
  getProductAPI,
  getDraftAPI,
  getFavoriteAPI,
  getHistoryAPI,
  getLikeAPI,
} from '@/services/center'
import { loginOutAPI } from '@/services/register'
import { ref } from 'vue'
import type { product } from '@/types/center'
import { formatTime } from '@/utils/time'
const { safeAreaInsets } = uni.getSystemInfoSync()
const memberStore = useMemberStore()
const index = ref(0)
const productList = ref<product[]>()
//滑动轮播图触发
const SwiperChange: UniHelper.SwiperOnChange = (ev) => {
  index.value = ev.detail.current
  if (memberStore.profile) {
    if (index.value == 0) {
      getProduct()
    } else if (index.value == 1) {
      getFavourate()
    } else if (index.value == 2) {
      getDraft()
    } else if (index.value == 3) {
      getLike()
    } else {
      getHistory()
    }
  }
}
const toLogin = () => {
  uni.navigateTo({ url: '/pages/login/login' })
}
const productnum = ref(0)
const getProduct = async () => {
  index.value = 0

  if (!memberStore.profile) {
    return
  }
  productList.value = []
  const res = await getProductAPI(memberStore.profile?.user.account)
  productList.value = res.data
  productnum.value = productList.value.length
}
const favouratenum = ref(0)
const getFavourate = async () => {
  index.value = 1

  productList.value = []
  if (!memberStore.profile) {
    return
  }
  const res = await getFavoriteAPI(memberStore.profile?.user.account)
  productList.value = res.data
  favouratenum.value = productList.value.length
}
const getDraft = async () => {
  index.value = 2

  productList.value = []
  if (!memberStore.profile) {
    return
  }

  const res = await getDraftAPI(memberStore.profile?.user.account)
  productList.value = res.data
  console.log(productList.value)
}
const getLike = async () => {
  index.value = 3

  productList.value = []
  if (!memberStore.profile) {
    return
  }

  const res = await getLikeAPI(memberStore.profile?.user.account)
  productList.value = res.data
  console.log(productList.value)
}
const getHistory = async () => {
  index.value = 4

  if (!memberStore.profile) {
    return
  }
  productList.value = []
  const res = await getHistoryAPI(memberStore.profile?.user.account)
  productList.value = res.data
}
const modifyInfo = () => {
  uni.navigateTo({ url: '/pages/modify/modify' })
}
//右侧弹出窗
const popup = ref<UniHelper.UniPopup>()
const toggle = () => {
  popup.value!.open('right')
}
//点击作品
const productDetail = (item: product) => {
  if (index.value == 0 || index.value == 2) {
    uni.navigateTo({
      url: `/pages/modifynews/modifynews?param1=${item.newsId}`,
    })
  } else {
    uni.navigateTo({ url: `/pages/detail/detail?newsId=${item.newsId}` })
  }
}
//退出登录
const Loginout = async () => {
  const res = await loginOutAPI(memberStore.profile!.token)
  if (res.code == 200) {
    memberStore.clearProfile()
    uni.reLaunch({
      url: '/pages/my/my',
    })
    uni.navigateTo({ url: '/pages/login/login' })
    popup.value!.close()
  }
}
//修改密码
const repassword = () => {
  uni.navigateTo({ url: '/pages/alterpassword/alterpassword' })
}

onLoad(() => {
  // uni.reLaunch({
  //   url: '/pages/my/my',
  // })
  getProduct()
})
onTabItemTap(() => {
  uni.reLaunch({
    url: '/pages/my/my',
  })
  getProduct()
})
</script>

<style lang="scss">
page {
  height: 100%;
}
.viewpoint {
  display: flex;
  flex-direction: column;
  height: 100%;
  .userInfo::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: url(https://pr1-greenteacdn.lenovo.com.cn/config/1672128620406_29.png) no-repeat;
    background-size: 100% 100%;
    filter: blur(4px);
    z-index: -1;
    background-size: cover;
  }
  .userInfo {
    position: relative;
    height: 450rpx;
    width: 100%;

    padding: 0 100rpx;
    .Info {
      height: 120rpx;
      width: 100%;
      display: flex;

      .avatar {
        height: 120rpx;
        width: 120rpx;
        border-radius: 120rpx;
        overflow: hidden;
      }
      .Info_item {
        padding: 10rpx 0;
        margin-left: 20rpx;
        height: 120rpx;
        flex: 1;
        display: flex;
        justify-content: space-around;
        flex-direction: column;
        .A,
        .B {
          color: #fff;
          width: 100%;
        }
        .A {
          font-weight: bold;
        }
        .B {
          font-size: 28rpx;
        }
      }
    }
    .setting {
      top: 60rpx;
      right: 60rpx;
      position: absolute;
      .iconfont {
        font-size: 60rpx;
        color: #fff;
        font-weight: bold;
      }
    }
  }
  .tabBar {
    height: 100rpx;
    width: 100%;
    display: flex;
    justify-content: space-around;
    view {
      height: 100%;
      width: 90rpx;
      text-align: center;
      line-height: 100rpx;
    }
    .active {
      border-bottom: 10rpx solid #f04142;
    }
  }
  .produce {
    background-color: #f2ecec;
    flex: 1;
    width: 100%;
    padding: 0 20rpx;
    .empty_swiper {
      display: flex;
      justify-content: center;
      align-items: center;
      flex-direction: column;
      height: 100%;
      width: 100%;
      .defaulimg_swiper {
        margin-bottom: 30rpx;
        width: 120rpx;
        height: 120rpx;
        border-radius: 100rpx;
        overflow: hidden;
      }
      .unlogin_swiper {
        width: 130rpx;
        height: 50rpx;
        font-size: 28rpx;
        background-color: #b1afaf;
        color: #fff;
        border-radius: 10rpx;
        text-align: center;
        line-height: 50rpx;
        font-weight: bold;
      }
    }
    .works {
      width: 100%;
      height: 290rpx;
      background-color: #fff;
      padding: 15rpx 15rpx 15rpx 15rpx;
      border-radius: 10rpx;
      margin-top: 10rpx;
      .user {
        width: 100%;
        height: 80rpx;
        display: flex;

        .avatar {
          height: 100%;
          width: 80rpx;
          border-radius: 80rpx;
          overflow: hidden;
        }
        .userinfo {
          height: 100%;
          flex: 1;
          display: flex;
          justify-content: space-between;
          line-height: 80rpx;
          font-size: 28rpx;
          margin-left: 10rpx;
        }
      }
      .news {
        width: 100%;
        height: 180rpx;
        display: flex;
        position: relative;
        .static {
          position: absolute;
          left: 10rpx;
          bottom: 5rpx;
          font-size: 24rpx;
          height: 40rpx;
          width: 100rpx;
          text-align: center;
          line-height: 40rpx;

          color: #fff;
        }
        .P {
          background-color: #42cb42;
        }
        .D {
          background-color: rgb(197, 197, 4);
        }
        .title {
          font-weight: bold;
          height: 90rpx;
          flex: 1;
          margin-top: 10rpx;
          padding: 10rpx 20rpx;
          overflow: hidden;
          text-overflow: ellipsis;
          display: -webkit-box;
          -webkit-line-clamp: 2;
          line-clamp: 2;
          -webkit-box-orient: vertical;
        }
        .cover {
          height: 100%;
          width: 180rpx;
        }
      }
    }
  }
}
.contain {
  position: absolute;
  top: 120rpx;
  .profile {
    margin-top: 20rpx;
    font-size: 28rpx;
  }
  .C {
    color: #f2d7d7;
  }
  .introduce {
    margin-top: 20rpx;
    font-size: 28rpx;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 1;
    line-clamp: 1;
    -webkit-box-orient: vertical;
  }
  .address {
    margin-top: 10rpx;
    font-size: 28rpx;
  }
}
.empty {
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  height: 100%;
  width: 100%;
  .defaulimg {
    margin-bottom: 30rpx;
    width: 120rpx;
    height: 120rpx;
    border-radius: 100rpx;
    overflow: hidden;
  }
  .unlogin {
    width: 120rpx;
    height: 50rpx;
    font-size: 28rpx;
    background-color: #f04142;
    color: #fff;
    border-radius: 10rpx;
    text-align: center;
    line-height: 50rpx;
    font-weight: bold;
  }
}
.iconfont {
  font-size: 28rpx;
}
.setting_select {
  width: 400rpx;
  .setting_contain {
    margin: 0 20rpx 20rpx 20rpx;
    border-bottom: 1rpx solid #ccc;
  }
}
.Q {
  width: 100%;
  height: 100rpx;
  text-align: left;
  margin-left: 20rpx;
  line-height: 100rpx;
  .iconfont {
    font-size: 40rpx;
    margin-right: 30rpx;
    font-weight: bold;
  }
  .selection {
    font-size: 32rpx;
  }
}
</style>